﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MatrixWebApiCore.Common
{
    public class DateHelpter
    {
        /// <summary>
        /// 转换为国际时间戳
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static long Stamp(DateTime datetime)
        {
            DateTime timeStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            long s = (Int64)(datetime - timeStart).TotalSeconds;
            return s;
        }

        /// <summary>
        /// 将时间戳转换为日期类型
        /// </summary>
        /// <param name="stamp"></param>
        /// <returns></returns>
        public static DateTime StampToDateTime(long stamp)
        {
            DateTime timeStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            DateTime s = timeStart.AddSeconds(stamp);
            return s;
        }
 

        /// <summary>
        /// 获取日期的上周五日期
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime GetDateLastFriday(DateTime datetime)
        { 
            int wkInt = (int)datetime.DayOfWeek;
            if (wkInt == 0)
            {
                wkInt = 7;
            }
            int cha = -(wkInt + 2);
            return datetime.AddDays(cha);         
        }

        /// <summary>
        /// 查找上月最后一天日期
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime GetLastMonthLastDay(DateTime datetime)
        {
            return  datetime.AddDays(-datetime.Day);
        }

    }
}
